var util= require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
    tabs: [
     
      {title: '生效中',},
      {title: '已失效', },
      
    ],
    isShowFrom3:false,
    tabClass: ["", "", ""],
    coupons:[],
    currenStatus:0,//选中状态
  },
  showFrom(e){
    var param = e.target.dataset.param; 
    this.setData({ 
     
      isShowFrom3: param == 3 ? (!this.data.isShowFrom3) : false
    });
    
  },
  openThis(e){
    var that = this;
    
    var num = e.currentTarget.dataset.id
    let c =that.data.coupons
    for (let index = 0; index < c.length; index++) {
      const element =  c[index];
      if(num==element._id)
      element['show'] = element.show=='block'?"none":"block"
    }
    that.setData({
      coupons:that.data.coupons
    })
  },
  delete(e){
    var  that =this
    const id = e.currentTarget.dataset.id;
    const db = wx.cloud.database()
    wx.showModal({
      title: '提示',
      content: '确定要作废这张优惠券吗？',
      success: function (res) {
        if (res.confirm) {
        db.collection('couponsMarket').doc(id).remove({
          
          success: function(res) {
            that.onShow();
          }
        })
        }
      }
    })
  },
  zuofei(e){
    var  that =this
    const id = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '提示',
      content: '确定要作废这张优惠券吗？',
      success: function (res) {
        if (res.confirm) {
          const db = wx.cloud.database()
        
          db.collection('couponsMarket').doc(id).update({
            data:{
              status:0
            },
            success: function(res) {
              that.onShow()
            }
          })
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
   //切换状态
   onClick: function(e) {
    var that = this;
    
    that.setData({
      currenStatus:e.detail.key,
      coupons: [],
    });
  ///  console.log(`ComponentId:${e.detail.componentId},you selected:${e.detail.key}`);
   
    this.getMyCoupons(e.detail.key);
  },
  //跳转添加页面
  addcoupons(){
   wx.navigateTo({
     url: 'coupons_type',
   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { 
    this.getMyCoupons(0);
  },
  getMyCoupons: function () {
    var that = this;
    const db = wx.cloud.database()
    const _ =db.command
    let where ={}
    let dd  = new Date()
   if(that.data.currenStatus==0){
     where ={
      deadline : _.gt(dd),startTime:_.lt(dd),
        status :1
     }
   }else if(that.data.currenStatus==1){
    where ={
      
      
    }
  }
  
    db.collection('couponsMarket').where(where).orderBy("leixing","desc").orderBy("dateAdd","desc").get({
      success: res => {
        
      
        if (res.data.length > 0) {
          let dd = new Date()
          var coupons = res.data;
          var couponsshow = [] 
          for (let index = 0; index < coupons.length; index++) {
            var element = coupons[index];
           
            if(element.deadline<dd && element.status==1){
              
              element.status=0
            }
            element.show = 'none'
            element.deadline = element.deadline?util.formatTime(element.deadline ):""
            element.startTime = element.startTime?util.formatTime(element.startTime ):""
            element.dateAdd = util.formatTime(element.dateAdd )
           
            if(element.status== that.data.currenStatus==0?1:0){
              couponsshow.push(element)
            }
          }
         console
         .log(coupons)
         
          if (coupons.length > 0) {
            that.setData({
              coupons: couponsshow,
            });
          }
         
        }else{
          that.setData({
            coupons: null,
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})